package com.jagochan.module.business.controller;

import com.jagochan.common.result.ResultUtils;
import com.jagochan.common.result.ResultVo;
import com.jagochan.module.business.pojo.po.Category;
import com.jagochan.module.business.pojo.enums.StatusEnum;
import com.jagochan.module.business.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "分类")
@RequiredArgsConstructor
@RequestMapping("/community/category")
@RestController
public class CategoryController {

    private final CategoryService categoryService;

    @Operation(summary = "查询分类列表")
    @GetMapping("/{type}")
    public ResultVo<List<Category>> listAll(@PathVariable @Parameter(description = "分类类型(topic或article)", example = "topic")
                                            String type) {
        if (StringUtils.isEmpty(type) || !"topic".equals(type) && !"article".equals(type)) {
            return ResultUtils.error("分类类型（article或topic）必填", null);
        }

        List<Category> poList = categoryService.lambdaQuery()
                .eq(Category::getType, type).eq(Category::getStatus, StatusEnum.ENABLE)
                .list();

        return ResultUtils.success("查询成功", poList);
    }

}